﻿<html>
  <script type="text/javascript" src="../js/photozoom.js">
  </script>
  <script type="text/javascript">

    //localStorage auf Anfrage an ContentScript senden
    chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse)
    {
      switch(request.method)
      {
        case "getOptions":
          sendResponse({value: localStorage});
          break;
          
        case "readFile":
          getFile(sendResponse, request.url);
          break;
          
        default:
          sendResponse({});
          break;
      }
    });
    
    function getFile(sendResponse, url)
    {
      var xhr = new XMLHttpRequest();
      
      xhr.onreadystatechange = function(data)
      {
        if (xhr.readyState == 4)
        {
          if (xhr.responseText != "")
            sendResponse({value: xhr.responseText});
          else
            sendResponse({value: null});
        }
      }
      
      xhr.open('GET', url, true);
      xhr.send();
    }
    
    //Kontextmenü-Eintrag erstellen
    var PhotoZoom_KontextMenu = chrome.contextMenus.create
    ({
      "title": "Großes Bild öffnen",
      "contexts": ["image"],
      "documentUrlPatterns": ["https://www.kwick.de/*", "http://www.kwick.de/*"],
      "onclick": PhotoZoom_OnKontextMenuClicked
    });
    //Kontextmenü Event
    function PhotoZoom_OnKontextMenuClicked(info, tab)
    {
      var url = info.srcUrl;

      if(PhotoZoom_IsZoomable(info.srcUrl))
        url = PhotoZoom_GetFullImg(info.srcUrl);

      //Get Tab index
      chrome.tabs.getSelected(null, function(tab)
      {
        var userIndex = 0;
        var selected = localStorage["photozoom_tabfocus"] == 1 ? true : false;

        switch(localStorage["photozoom_tablocation"])
        {
          case "-":
            userIndex = 0;
            break;
          case "0":
            userIndex = tab.index;
            break;
          case "1":
            userIndex = tab.index + 1;
            break;
          case "+":
            userIndex = null;
            break;
        }

        if(userIndex != null)
          chrome.tabs.create({"index": userIndex, "url": url, "selected": selected});
        else
          chrome.tabs.create({"url": url, "selected": selected});
      })
    }

  </script>
</html>